package cn.house.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileAction extends ActionSupport {

	private File file;
	private String fileContentType;// 文件类型
	private String fileFileName;// 文件名字

	private String showFileName;// 要显示的图片名
	private String downLoadFile;//下载文件名

	public String upload() {
		try {
			fileFileName = System.currentTimeMillis() + getFileFileName();
			// 保存文件
			IOUtils.copy(new FileInputStream(getFile()), new FileOutputStream("E:\\static\\" + fileFileName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "upload_success";
	}

	// 显示图片
	public void show() {
		try {
			//读取
			FileInputStream fis = new FileInputStream("E:\\static\\"+getShowFileName());
			byte[] bs = new byte[fis.available()];
			fis.read(bs);
			
			//输出
			ServletActionContext.getResponse().getOutputStream().write(bs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//下载
	public void download(){
		try {
			//读取
			FileInputStream fis = new FileInputStream("E:\\static\\"+getDownLoadFile());
			byte[] bs = new byte[fis.available()];
			fis.read(bs);
			fis.close();
			
			//下载的类型
			ServletActionContext.getResponse().setContentType("application/octet-stream");
			ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;filename="+getDownLoadFile());
			
			//输出
			ServletActionContext.getResponse().getOutputStream().write(bs);
//			IOUtils.copy(fis, ServletActionContext.getResponse().getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getShowFileName() {
		
		return showFileName;
	}

	public void setShowFileName(String showFileName) {
		this.showFileName = showFileName;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getDownLoadFile() {
		return downLoadFile;
	}

	public void setDownLoadFile(String downLoadFile) {
		this.downLoadFile = downLoadFile;
	}

}
